// StringRegisterFunction.cpp: implementation of the StringRegisterFunction class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "StringRegisterFunction.h"
#include "partString.h"
#include "part.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNAMIC_EX(StringRegisterFunction, FunctionObject)

StringRegisterFunction::StringRegisterFunction()
{
	m_pNextRegister = NULL;
}

StringRegisterFunction::~StringRegisterFunction()
{

}

void StringRegisterFunction::RegisterToString()
{
	Part* pPart = GetOwner();
	ASSERT(pPart);
	PartString* pString = pPart->GetOwner();
	ASSERT(pString);

	FunctionObject* pFunction = NULL;
	StringRegisterFunction* pRegister = NULL;
	pFunction = pString->GetFunction(this->GetFunctionStyle());
	if( pFunction != NULL)
	{
		pRegister = static_cast<StringRegisterFunction*>(pFunction);
		pRegister->m_pNextRegister = this;
		return;
	}

	pString->AttachFunction(this);
}

void StringRegisterFunction::UnRegisterToString()
{
	Part* pPart = GetOwner();
	ASSERT(pPart);
	PartString* pString = pPart->GetOwner();
	ASSERT(pString);

	FunctionObject* pFunction = NULL;
	StringRegisterFunction* pRegister = NULL;
	pFunction = pString->GetFunction(this->GetFunctionStyle());
	if( pFunction != NULL)
	{
		pRegister = static_cast<StringRegisterFunction*>(pFunction);
		if(pRegister == this)
		{
			pString->RemoveFunction(GetRegisterInfo()->GetKey());
			pString->AttachFunction(pRegister->m_pNextRegister);
			return;
		}
		else if(pRegister->m_pNextRegister == this)
		{
			pRegister->m_pNextRegister = m_pNextRegister;
			return;
		}
		pFunction = pRegister->m_pNextRegister;
	}

}

INT StringRegisterFunction::GetRegisterFunctionNum()
{
	INT nCount = 0;
	Part* pPart = GetOwner();
	ASSERT(pPart);
	PartString* pString = pPart->GetOwner();
	ASSERT(pString);
	
	StringRegisterFunction* pRegister = NULL;
	FunctionObject* pFunction = pString->GetFunction(this->GetFunctionStyle());
	while(pFunction != NULL)
	{
		nCount++;
		pRegister = static_cast<StringRegisterFunction*>(pFunction);
		pFunction = pRegister->m_pNextRegister;
	}

	return nCount;
}